<?
session_start();
require "../util/DBUtil.php";
/**
 * 购物车
 */

?>
<link rel="stylesheet" href="../css/bootstrap.min.css">
<script src="../js/compute_money.js"></script>
<script src="../js/pay_form.js"></script>
<!--支付表单样式-->
<style>
    div.form{
        position: absolute;
        width: 100%;
        height: 100%;
        top: 0;
        left: 0;
        background-color: rgba(0,0,0,0.6);
        z-index: 5;

    }
    div.display{
        display: none;
    }
    form.pay_form{
        position: relative;
        width: 320px;
        left: 50%;
        top: 50%;
        margin: -103px 0 0 -160px;
        border: 30px #f5f5f5 solid;
        padding: 15px;
        background-color: white;

    }
    form.pay_form>input,input.price{
        width: 50px;
        margin: 6px 0;
    }
</style>
<div class="container">
    <div class="row">
        <?php
        if (!isset($_GET['bookNo']) && !$_SESSION['login_user']) {
            echo "<h4>购物车内暂时没有商品<br><a href='../index.php'>去购物></a></h4>";
            return;
        }
        if (!$_SESSION['login_user']) {
            ?>
            <div class="alert alert-warning" role="alert">
                <span class="glyphicon glyphicon-alert" aria-hidden="true"></span>
                您还没有登录！登录后购物车的商品将保存到您账号中 <a href="">请先登录</a></div>
            <?php
        } ?>
    </div>
    <div class="row">
        <table class="table" id="table">
            <tr>
                <td class="active">全选</td>
                <td class="active">商品</td>
                <td class="active">单价</td>
                <td class="active">数量</td>
                <td class="active">小计(元)</td>
                <td class="active">操作</td>
            </tr>
            <?php
            $db = new DBUtil();
            //没有登录
            if (!$_SESSION['login_user']) {
                $bookNo = $_GET['bookNo'];
                $sql = "select * from book where bookNo = ? ";
                $pram = array($bookNo);
                $bookInfo = $db->query($sql, $pram)[0];
                ?>
                <tr id="display">
                    <td><input onchange="change(this)" class="bookNo" type="checkbox" checked value=<?php echo $bookInfo['bookNo'] ?>></td>
                    <td>
                        <img src=<?php echo '../img/' . $bookInfo['picture'] ?> alt=""><?php echo $bookInfo['bookName'] ?>
                    </td>
                    <td><?php echo $bookInfo['price'] ?></td>
                    <td><input type="number" min="1" max="199" value=<?php echo $_GET['bookNum'] ?>></td>
                    <td class="price"><?php echo $bookInfo['price'] * $_GET['bookNum']; ?></td>
                    <td><a href="#" onclick="delete_tr()">删除</a></td>
                </tr>

                <?php
            } //登录的用户
            else {
                $sql = "select book.*,item.id,item.bookNum,item.onePrice from item,book where book.bookNo = item.bookNo and item.userID=? and item.pay=0";
                $pram = array("1");
                $bookInfos = $db->query($sql, $pram);
                foreach ($bookInfos as $bookInfo) {
                    ?>
                    <tr>
                        <td><input onchange="change(this)" class="bookNo" type="checkbox" checked value=<?php echo $bookInfo['id'] ?>></td>
                        <td>
                            <img src=<?php echo '../img/' . $bookInfo['picture'] ?> alt=""><?php echo $bookInfo['bookName'] ?>
                        </td>
                        <td><?php echo $bookInfo['onePrice'] ?></td>
                        <td><input type="number" min="1" max="199" value=<?php echo $bookInfo['bookNum'] ?>></td>
                        <td class="price"><?php echo $bookInfo['onePrice'] * $bookInfo['bookNum']; ?></td>
                        <td><a href="../action/delete_cart.php?id=<?php echo $bookInfo['id'] ?>">删除</a></td>
                    </tr>
                    <?php
                }
            }
            ?>

        </table>
    </div>
    <div class="row">
        <input onchange="change(this)" type="checkbox" checked id="checkAll">全选
        <div style="float: right">
            <i >合计 <span style="font-size: 20px;color: #f42424" id="show_money"></span>元</i>
            <button type="button" class="btn btn-danger" onclick="pay()" id="btn_pay">立即支付</button>
        </div>
    </div>
</div>

<!--支付表单-->
<div class="form display" id="display">
    <form action="../action/pay_OK.php" onsubmit="return pay_OK()" method="post" class="pay_form">
        <input type="hidden" id="bookNos" name="bookNos">
        <lable>支付金额：<input class="price" type="text" disabled name="price" id="price" value="">元</lable><br>
        <input type="password" maxlength="1" name="password0" class="pwd">
        <input type="password" maxlength="1" name="password1" class="pwd">
        <input type="password" maxlength="1" name="password2" class="pwd">
        <input type="password" maxlength="1" name="password3" class="pwd"><br>
        <button type="button" onclick="document.getElementById('display').className='form display'" class="btn btn-default">取消</button>
        <button type="submit"  class="btn btn-primary">确认支付</button>
    </form>
</div>
